const {decode} = require("../utils")
module.exports = {
	cors(){
		return function(req,res,next){
			res.set("Access-Control-Allow-Origin","*");
			res.set("Access-Control-Allow-Headers","*");
			res.set("Access-Control-Allow-Methods","*");
			next();
		}
	},
	sendJson(){
		return function(req,res,next){
			res.sendJson = function(msg="网络连接错误",ok=-1){
				res.json({
					ok,
					msg
				})
			}
			next();
		}
	},
	author(){
		return (req,res,next)=>{
			// 获取到token
			decode(req.headers.token).then(()=>{
				next();
			}).catch(err=>{
				res.sendJson("token异常",-2)
			})
		}
	}
}